package com.agv.web.adm;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.agv.core.objectDomain.AdmCatalogo;
import com.agv.core.objectDomain.Catalogo;
import com.agv.core.objectDomain.MenuSelect;
import com.agv.core.objectDomain.Modulo;
import com.agv.core.services.ICatalogosService;
import com.agv.core.services.ISecurityService;
import com.agv.core.util.Constantes;
import com.agv.web.MonitoringDispatchAction;

public class AdmDispatchAction extends MonitoringDispatchAction {

	private ICatalogosService catalogosService;

	private ISecurityService securityService;

	public ActionForward cfgCatalogos(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			String nbCatalogo = request.getParameter("nbCatalogo");
			AdmCatalogo admCatalogo = catalogosService
					.obtenerAdmCatalgo(nbCatalogo);
			ArrayList<Catalogo> catalogos = catalogosService
					.obtenerCatalgos(admCatalogo.getPasos());
			admCatalogo.setCatalogos(catalogos);

			request.getSession().removeAttribute("catCompleto");
			request.getSession().removeAttribute("catInsertado");
			request.getSession().removeAttribute("buscaCatalogo");

			request.getSession().setAttribute("listaAutocompletarObj",
					admCatalogo.getListaAutocompletar());
			request.getSession().setAttribute("catalogoObj", admCatalogo);
			request.getSession().setAttribute("altaCatalogo", "si");
			request.getSession().setAttribute("pasoActual", String.valueOf(1));
			request.getSession().setAttribute("totalPasos",
					new Integer(admCatalogo.getPasos().size()));
			return mapping.findForward("cfgCatalogos");
		} catch (Exception e) {
			monitoringError(request, e);
			log.error(e, e);
			return mapping.findForward(Constantes.FWD_ERR);
		}
	}

	public ActionForward cfgPerfiles(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			request.getSession().removeAttribute("catCompleto");
			request.getSession().removeAttribute("catInsertado");

			request.getSession().removeAttribute("catCompleto");
			request.getSession().removeAttribute("catInsertado");
			request.getSession().removeAttribute("catalogoObj");
			request.getSession().removeAttribute("pasoActual");
			request.getSession().removeAttribute("totalPasos");
			request.getSession().removeAttribute("buscaCatalogo");
			request.getSession().removeAttribute("ruta");
			request.getSession().removeAttribute("modulo");

			ArrayList<MenuSelect> menuSelect = securityService
					.obtenerMenuSelect("nb_estatus", "id_estatus",
							"empresa.emp014_estatus");
			ArrayList<Modulo> modulos = securityService
					.obtenerFuncionesPorModulo();

			request.getSession().setAttribute("menuSelect", menuSelect);
			request.getSession().setAttribute("modulosObj", modulos);
			request.getSession().setAttribute("altaCatalogo", "si");

			return mapping.findForward("cfgPerfiles");
		} catch (Exception e) {
			monitoringError(request, e);
			log.error(e, e);
			return mapping.findForward(Constantes.FWD_ERR);
		}
	}

	public void setCatalogosService(ICatalogosService catalogosService) {
		this.catalogosService = catalogosService;
	}

	/**
	 * @param securityService
	 *            the securityService to set
	 */
	public void setSecurityService(ISecurityService securityService) {
		this.securityService = securityService;
	}

}
